/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.DAO;

import com.aptech.model.RealEstateType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Hoang Quy Hong An
 */
public class realEstateTypeDAO {

    private  Connection myCon = null;
    private  PreparedStatement ps = null;
    private  ResultSet rs = null;
    private  String strCommand = "";

    public  ArrayList<RealEstateType> getAllRealEstateType() {
        System.out.println("Goi den day lan thu nhat!");
        ArrayList<RealEstateType> listRealStateType = new ArrayList<RealEstateType>();
        RealEstateType ret = null;
        strCommand = "SELECT realStateTypeID ,realStateTypeName "
                + "FROM tbl_RealStateType WHERE realStateTypeStatus = 1";
        try {
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = ps.executeQuery();
            while (rs.next()) {
                int realStateTypeID = rs.getInt("realStateTypeID");
                String realStateTypeName = rs.getString("realStateTypeName");
                ret = new RealEstateType(realStateTypeID, realStateTypeName);
                listRealStateType.add(ret);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listRealStateType;
    }

    public  String getRealEstateTypeByID(int realStateTypeID) {
        String realStateTypeName ="";
        ArrayList<RealEstateType> listRealStateType = new ArrayList<RealEstateType>();
        RealEstateType ret = null;
        strCommand = "SELECT realStateTypeName "
                + "FROM tbl_RealStateType WHERE realStateTypeID = ?";
        try {
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, realStateTypeID);
            rs = ps.executeQuery();
            if(rs.next()) {
                 realStateTypeName = rs.getString("realStateTypeName");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return realStateTypeName;
    }
}
